package cn.microservices.cloud.common.web.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 根据业务封装的获取redis的一些通用方法
 *
 * @author xiaobai
 */
@Component
public class RedisUtils<T>  {

    @Autowired
    @Qualifier("redis")
    RedisTemplate redisTemplate;


    /**
     * 添加值
     * @param key
     * @param value
     */
    public void putValue(String key,  String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 添加值 过期毫秒
     * @param key
     * @param value
     */
    public void putValue(String key,  String value,long time) {
        redisTemplate.opsForValue().set(key, value,time, TimeUnit.MILLISECONDS);
    }


    /**
     * 添加值 并设置过期时间
     * @param key
     * @param value
     */
    public void putValue(String key,  String value,long time,TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value,time,timeUnit);
    }

    /**
     * 获取 value
     *
     * @param key
     * @return
     */
    public Object getValue(String key) {
        Object obj = redisTemplate.opsForValue().get(key);
        return obj;
    }
// redisTemplate.opsForZSet().removeRange(key, rank, rank);
    /**
     * 删除
     *
     * @param key
     * @return
     */
    public Boolean delValue(String key) {
        Boolean flag = redisTemplate.delete(key);
        return flag;
    }

    /**
     * zset 删除
     *
     * @param key
     * @return
     */
    public Long zsetRemove(String key,Long val1,Long val2) {
         Long l = redisTemplate.opsForZSet().removeRange(key, val1, val2);
        return l;
    }
    /**
     * 添加hash 值
     * @param key
     * @param hashKey
     * @param value
     */
    public void putHashValue(String key, String hashKey, String value) {
        redisTemplate.opsForHash().put(key, hashKey,value);
    }


    /**
     * 获取hash value
     *
     * @param key
     * @param hashKey
     * @return
     */
    public Object getHashValue(String key, String hashKey) {
        Object obj = redisTemplate.opsForHash().get(key, hashKey);
        return obj;
    }







    public Map<String, T> getMapByKey(String key, Class<T> clazz) {
        Map<String, T> map = redisTemplate.opsForHash().entries(key);
        return map;
    }

    /**
     * 对象转化为map
     */
    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, String> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, String> map = new HashMap<String, String>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            if (value != null) {
                if (value instanceof Date) {
                    Long time = ((Date) value).getTime();
                    map.put(fieldName, time.toString());
                } else {
                    map.put(fieldName, value.toString());
                }
            }
        }
        return map;
    }
}
